package com.sdk.es.http.gen;

import cn.hutool.core.util.StrUtil;

/**
 * @author limaosheng
 * @create 2023-08-25 15:45
 * @desc
 */
public class GenUtil {
    //是否覆盖原文件
    public static final Boolean isOver = false;

    /**
     * 根据索引名称生成所有相关代码
     * @param args
     */
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer();
/*        Field[] fields = IndexConts.class.getFields();
        for (Field field : fields) {
            if (Modifier.isStatic(field.getModifiers())) {
                try {
                    String index = field.get(null).toString();
                    if(IndexConts.SAAS_DEVICE_STATUS_CHANGE.equals(index)){
                        index = "saas_device_status_change";
                    }
                    gen(index,sb);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }*/
        gen("limit_request_times",sb);
        System.out.println(sb);
    }

    public static void gen(String index,StringBuffer sb){
        //生成基础entity，字段自行补充，并复制到api服务：com.digitallife.community.saas.es.entity.saas.base
        genEntity(index);
        //生成manager，并复制到api服务：com.digitallife.community.saas.es.api
        genManager(index,sb);
        //生成service
        genIService(index);
        genServiceImpl(index);
        //生成controller
        genController(index);
        //生成kafkaConsumer
        genConsumer(index);
        //生成测试用例
        genApiTest(index);

    }
    private static void genEntity(String index) {
        StringBuffer sb = new StringBuffer();
        sb.append("package com.digitallife.community.elasticsearch.gen.entity;\n" +
                "\n" +
                "import com.digitallife.community.saas.es.constants.IndexConts;\n" +
                "import com.digitallife.community.saas.es.entity.common.EsBaseEntity;\n" +
                "import com.fasterxml.jackson.annotation.JsonIgnoreProperties;\n" +
                "import com.fasterxml.jackson.annotation.JsonInclude;\n" +
                "import lombok.Data;\n" +
                "import lombok.EqualsAndHashCode;\n" +
                "import lombok.experimental.Accessors;\n" +
                "import org.springframework.data.elasticsearch.annotations.Document;\n" +
                "@Document(indexName = IndexConts." + StrUtil.swapCase(index) + " + \"_#{@esIndexNameGenerator.getYearMonthStr()}\")\n" +
                "@Data\n" +
                "@Accessors(chain = true)\n" +
                "@EqualsAndHashCode(callSuper = true)\n" +
                "@JsonInclude(JsonInclude.Include.NON_NULL)\n" +
                "@JsonIgnoreProperties(ignoreUnknown = true)\n" +
                "public class "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +" extends EsBaseEntity {" +
                "    //补充实体字段信息\n" +
                "}"
                );
        String path = System.getProperty("user.dir") + "\\common\\src\\main\\java\\com\\digitallife\\community\\elasticsearch\\gen\\entity\\"
                + StrUtil.upperFirst(StrUtil.toCamelCase(index)) + ".java";
        FileUtil.write(path, sb.toString(),isOver);

    }
    private static void genManager(String index,StringBuffer sb1) {
        StringBuffer sb = new StringBuffer();
        sb.append("package com.digitallife.community.elasticsearch.gen.manager;\n" +
                "\n" +
                "import com.digitallife.community.saas.es.entity.saas.base."+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +";\n" +
                "import org.springframework.beans.factory.annotation.Value;\n" +
                "import org.springframework.stereotype.Component;\n" +
                "import com.digitallife.community.saas.es.api.base.EsBaseHttpManager;\n" +
                "\n" +
                "@Service\n" +
                "public class Es"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"Manager extends EsBaseHttpManager<"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +">{\n" +
                "\n" +
                "    @Value(\"${saas.inner.url}/community/elasticsearch/saas/"+ StrUtil.toCamelCase(index) +"/basic\")\n" +
                "    protected String URL_PREFIX;\n" +
                "    @Override\n" +
                "    protected String getUrl() {\n" +
                "        return URL_PREFIX;\n" +
                "    }" +
                "}\n");
        System.out.println("com.digitallife.community.saas.es.api.Es"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"Manager,\\");
        sb1.append("com.digitallife.community.saas.es.api.Es"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"Manager,\\\n");
        String path = System.getProperty("user.dir") + "\\common\\src\\main\\java\\com\\digitallife\\community\\elasticsearch\\gen\\manager\\Es"
                + StrUtil.upperFirst(StrUtil.toCamelCase(index)) + "Manager.java";
        FileUtil.write(path, sb.toString(),isOver);
    }

    private static void genApiTest(String index) {
        StringBuffer sb = new StringBuffer();
        sb.append("package api;\n" +
                "\n" +
                "import com.digitallife.community.saas.es.entity.common.EsBaseParams;\n" +
                "import com.digitallife.community.saas.es.entity.common.EsPageReq;\n" +
                "import com.digitallife.community.saas.es.entity.saas.base."+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +";\n" +
                "import org.junit.jupiter.api.Test;\n" +
                "import util.ApiUtil;\n" +
                "\n" +
                "import java.util.ArrayList;\n" +
                "import java.util.List;\n" +
                "\n" +
                "public class "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"ControllerTest {\n" +
                "\n" +
                "    final static String URL = \"/community/elasticsearch/saas/"+ StrUtil.toCamelCase(index) +"/basic\";\n" +
                "\n" +
                "    @Test\n" +
                "    public void save(){\n" +
                "        "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +" req = new "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"();\n" +
                "        ApiUtil.local(URL + \"/save\",req);\n" +
                "    }\n" +
                "\n" +
                "    @Test\n" +
                "    public void saveAsync(){\n" +
                "        "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +" req = new "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"();\n" +
                "        ApiUtil.local(URL + \"/saveAsync\",req);\n" +
                "    }\n" +
                "\n" +
                "    @Test\n" +
                "    public void saveBatch(){\n" +
                "        List<"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"> reqList = new ArrayList<>();\n" +
                "        "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +" req = new "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"();\n" +
                "        reqList.add(req);\n" +
                "        ApiUtil.local(URL + \"/saveBatch\",reqList);\n" +
                "    }\n" +
                "\n" +
                "    @Test\n" +
                "    public void saveBatchAsync(){\n" +
                "        List<"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"> reqList = new ArrayList<>();\n" +
                "        "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +" req = new "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"();\n" +
                "        reqList.add(req);\n" +
                "        ApiUtil.local(URL + \"/saveBatchAsync\",reqList);\n" +
                "    }\n" +
                "\n" +
                "    @Test\n" +
                "    public void update(){\n" +
                "        "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +" req = new "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"();\n" +
                "        ApiUtil.local(URL + \"/update\",req);\n" +
                "    }\n" +
                "\n" +
                "    @Test\n" +
                "    public void updateAsync(){\n" +
                "        "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +" req = new "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"();\n" +
                "        ApiUtil.local(URL + \"/updateAsync\",req);\n" +
                "    }\n" +
                "\n" +
                "    @Test\n" +
                "    public void delete(){\n" +
                "        "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +" req = new "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"();\n" +
                "        ApiUtil.local(URL + \"/delete\",req);\n" +
                "    }\n" +
                "\n" +
                "    @Test\n" +
                "    public void deleteAsync(){\n" +
                "        "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +" req = new "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"();\n" +
                "        ApiUtil.local(URL + \"/deleteAsync\",req);\n" +
                "    }\n" +
                "\n" +
                "    @Test\n" +
                "    public void getById(){\n" +
                "        "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +" req = new "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"();\n" +
                "        req.setId(\"\");\n" +
                "        ApiUtil.local(URL + \"/getById\",req);\n" +
                "    }\n" +
                "\n" +
                "    @Test\n" +
                "    public void getOne(){\n" +
                "        EsBaseParams<"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"> req = new EsBaseParams(new "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"());\n" +
                "        ApiUtil.local(URL + \"/getOne\",req);\n" +
                "    }\n" +
                "\n" +
                "    @Test\n" +
                "    public void getList(){\n" +
                "        EsBaseParams<"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"> req = new EsBaseParams(new "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"());\n" +
                "        ApiUtil.local(URL + \"/getList\",req);\n" +
                "    }\n" +
                "\n" +
                "    @Test\n" +
                "    public void page(){\n" +
                "        EsPageReq<"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"> pageReq = new EsPageReq();\n" +
                "        pageReq.setPageNum(1);\n" +
                "        pageReq.setPageSize(10);\n" +
                "        EsBaseParams<"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"> req = new EsBaseParams(new "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"());\n" +
                "        pageReq.setReq(req);\n" +
                "        ApiUtil.local(URL + \"/page\",pageReq);\n" +
                "    }\n" +
                "\n" +
                "    @Test\n" +
                "    public void count(){\n" +
                "        EsBaseParams<"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"> req = new EsBaseParams(new "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"());\n" +
                "        ApiUtil.local(URL + \"/count\",req);\n" +
                "    }\n" +
                "    @Test\n" +
                "    public void groupBy(){\n" +
                "        EsBaseParams<"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"> req = new EsBaseParams(new "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"());\n" +
                "        ApiUtil.local(URL + \"/groupBy\",req);\n" +
                "    }\n" +
                "\n" +
                "    @Test\n" +
                "    public void groupByPage(){\n" +
                "        EsPageReq<"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"> pageReq = new EsPageReq();\n" +
                "        pageReq.setPageNum(1);\n" +
                "        pageReq.setPageSize(10);\n" +
                "        EsBaseParams<"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"> req = new EsBaseParams(new "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"());\n" +
                "        pageReq.setReq(req);\n" +
                "        ApiUtil.local(URL + \"/groupByPage\",pageReq);\n" +
                "    }" +
                "}");
        String path = System.getProperty("user.dir") + "\\elasticsearch-service\\src\\test\\java\\api\\"
                + StrUtil.upperFirst(StrUtil.toCamelCase(index)) + "ControllerTest.java";
        FileUtil.write(path, sb.toString(),isOver);
    }

    private static void genConsumer(String index) {
        StringBuffer sb = new StringBuffer();
        sb.append("package com.digitallife.community.elasticsearch.mq.saas;\n" +
                "\n" +
                "import com.digitallife.community.elasticsearch.conts.Conts;\n" +
                "import com.digitallife.community.elasticsearch.mq.EsBaseConsumer;\n" +
                "import com.digitallife.community.saas.es.constants.IndexConts;\n" +
                "import com.digitallife.community.saas.es.entity.saas.base."+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +";\n" +
                "import lombok.extern.slf4j.Slf4j;\n" +
                "import org.springframework.kafka.annotation.KafkaListener;\n" +
                "import org.springframework.kafka.support.KafkaHeaders;\n" +
                "import org.springframework.messaging.handler.annotation.Header;\n" +
                "import org.springframework.messaging.handler.annotation.Payload;\n" +
                "import org.springframework.stereotype.Component;\n" +
                "import java.util.List;" +
                "\n" +
                "/**\n" +
                " * @desc 注：修改和删除业务极少，默认关闭监听，需要再打开\n" +
                " */\n" +
                "@Component\n" +
                "@Slf4j\n" +
                "public class "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"Consumer extends EsBaseConsumer<"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"> {\n" +
                "    /**\n" +
                "     * 初始化基础索引名称\n" +
                "     */\n" +
                "    @Override\n" +
                "    protected String initEsIndex() {\n" +
                "        return IndexConts." + StrUtil.swapCase(index) + ";\n" +
                "    }\n" +
                "\n" +
                "    @Override\n" +
                "    @KafkaListener(topics = {\n" +
                "            Conts.TOPIC_PREFIX + IndexConts." + StrUtil.swapCase(index) + " + Conts.TOPIC_SAVE_SUFFIX\n" +
                "            //,Conts.TOPIC_PREFIX +IndexConts." + StrUtil.swapCase(index) + " + Conts.TOPIC_UPDATE_SUFFIX\n" +
                "            //,Conts.TOPIC_PREFIX + IndexConts." + StrUtil.swapCase(index) + " + Conts.TOPIC_DELETE_SUFFIX\n" +
                "    },concurrency = \"1\")\n" +
                "    protected void listener(@Payload List<String> messageList, @Header(KafkaHeaders.RECEIVED_TOPIC) List<String> topicList) {\n" +
                "        super.listener(messageList, topicList);\n" +
                "    }\n" +
                "}"
        );
        String path = System.getProperty("user.dir") + "/data-manipulation-service\\src\\main\\java\\com\\digitallife\\community\\elasticsearch\\mq\\saas\\"
                + StrUtil.upperFirst(StrUtil.toCamelCase(index)) + "Consumer.java";
        FileUtil.write(path, sb.toString(),isOver);
    }

    private static void genController(String index) {
        StringBuffer sb = new StringBuffer();
        sb.append("package com.digitallife.community.elasticsearch.controller.saas;\n" +
                "\n" +
                "import com.digitallife.community.elasticsearch.controller.EsBaseController;\n" +
                "import com.digitallife.community.elasticsearch.service.I"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"Service;\n" +
                "import com.digitallife.community.saas.es.entity.saas.base."+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +";\n" +
                "import lombok.extern.slf4j.Slf4j;\n" +
                "import org.springframework.web.bind.annotation.RequestMapping;\n" +
                "import org.springframework.web.bind.annotation.RestController;\n" +
                "\n" +
                "@Slf4j\n" +
                "@RequestMapping(\"/saas/"+ StrUtil.toCamelCase(index) +"/basic\")\n" +
                "@RestController\n" +
                "public class "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"Controller extends EsBaseController<"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +", I"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"Service> {\n" +
                "\n" +
                "}\n");
        String path = System.getProperty("user.dir") + "/elasticsearch-service\\src\\main\\java\\com\\digitallife\\community\\elasticsearch\\controller\\saas\\"
                + StrUtil.upperFirst(StrUtil.toCamelCase(index)) + "Controller.java";
        FileUtil.write(path, sb.toString(),isOver);
    }


    private static void genServiceImpl(String index) {
        StringBuffer sb = new StringBuffer();
        sb.append("package com.digitallife.community.elasticsearch.service.impl;\n" +
                "\n" +
                "import com.digitallife.community.elasticsearch.service.I"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"Service;\n" +
                "import com.digitallife.community.elasticsearch.service.base.impl.EsBaseServiceImpl;\n" +
                "import com.digitallife.community.saas.es.entity.saas.base."+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +";\n" +
                "import lombok.extern.slf4j.Slf4j;\n" +
                "import org.springframework.stereotype.Service;\n" +
                "import com.digitallife.community.saas.es.constants.IndexConts;\n" +
                "\n" +
                "@Slf4j\n" +
                "@Service\n" +
                "public class "+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"ServiceImpl extends EsBaseServiceImpl<"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"> implements I"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"Service {\n" +
                "\n" +
                "    /**\n" +
                "     * 指定基础索引名，需要在子类复写\n" +
                "     */\n" +
                "    @Override\n" +
                "    protected String getEsIndex() {\n" +
                "        return IndexConts."+StrUtil.swapCase(index)+";\n" +
                "    }\n" +
                "}");
        String path = System.getProperty("user.dir") + "/common/src/main/java/com/digitallife/community/elasticsearch/service/impl/"
                + StrUtil.upperFirst(StrUtil.toCamelCase(index)) + "ServiceImpl.java";
        FileUtil.write(path, sb.toString(),isOver);
    }


    private static void genIService(String index) {
        StringBuffer sb = new StringBuffer();
        sb.append("package com.digitallife.community.elasticsearch.service;\n" +
                "\n" +
                "\n" +
                "import com.digitallife.community.elasticsearch.service.base.IEsBaseService;\n" +
                "import com.digitallife.community.saas.es.entity.saas.base."+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +";\n" +
                "\n" +
                "public interface I"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"Service extends IEsBaseService<"+ StrUtil.upperFirst(StrUtil.toCamelCase(index)) +"> {\n" +
                "\n" +
                "}");
        String path = System.getProperty("user.dir") + "/common/src/main/java/com/digitallife/community/elasticsearch/service/I"
                + StrUtil.upperFirst(StrUtil.toCamelCase(index)) + "Service.java";
        FileUtil.write(path, sb.toString(),isOver);
    }
}
